# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 18:46:42 2020

@author: Farman
"""

import sys
import pathlib

# prepare SampleMan
try:
    import SampleMan
except:
    cur_dir = pathlib.Path(sys.argv[0]).parent.parent
    sys.path.append(str(cur_dir/'ClassificationSamples'))
    import SampleMan
    
#=============================================================================

# prepare WRSMan

try:
    import WRSMan
except:
    cur_dir = pathlib.Path(sys.argv[0]).parent.parent
    sys.path.append(str(cur_dir/'WRS'))
    import WRSMan

#print(wrs_man.get_path_row(0,0))

#=============================================================================

# prepare utm

import utm

#=============================================================================

# prepare GeotiffMan for landsat data locating

try:
    import GeotiffMan
except:
    cur_dir = pathlib.Path(sys.argv[0]).parent.parent
    sys.path.append(str(cur_dir/'GeoTiffOperate'))
    import GeotiffMan

#=============================================================================

import LandsatFileListMan

#=============================================================================
landsat_files_dump_file = r'LandsatFileDictInPathRowYear.joblib'
landsat_files_man = LandsatFileListMan.LandsatFilesMan(landsat_files_dump_file)
    
sample_man = SampleMan.ClassificationSamples(r'D:\CUIT-Farman\Code\ClassificationSamples\SamplesFromYu.joblib')

wrs_man = WRSMan.WRS(r'D:\CUIT-Farman\Code\WRS\wrs_control_points.joblib')

list_class_level_1 = sample_man.list_level_1

for level in list_class_level_1:
    pos_list = sample_man.get_pos_list_of_level_1(level)
    #print(pos_list)
    #print('=' * 20)
    
    for pos in pos_list:
        [lon, lat] = pos
        path, row = wrs_man.get_path_row(lon, lat)
        print(lon, lat, '->', path, row)
        
        files = landsat_files_man.get_files_of_path_row(path, row)
        if files == None:
            print('No such landsat data file.\n')
            continue
        else:
            print(files, '\n')
        
        x, y, *tail = utm.conversion.from_latlon(lat, lon)
        x , y = int(x+0.5), int(y+0.5)
        print(x, y, '\n')
        
        
        for file in files:
            file = pathlib.Path(r'D:\CUIT-Farman\Data\WuxiData\Wuxi-LandSat')/pathlib.Path(file).name
            file = str(file)
            
            try:
                geo_tiff = GeotiffMan.GeoTiffMan(file)`
                geo_tran = GeotiffMan.GeoTransform(geo_tiff.get_geo_transform())
                x, y = geo_tran.get_reverse_project(x, y)
                print('Pos in scene:', x, y)
                input('Press Enter to continue...')
            except:
                print('File not exist.')

list_class_level_2 = sample_man.list_level_2